#include <stdio.h>
#include <stdlib.h>

char *connectCharArray(const char *string_01, const char *string_02);

int main() {
    // 在 C 中, 字符串就是字符型数组, 即 char[]
    // 字符串 "0123456" 在字符型数组中实际存储的是 "0123456'\0'", 需要增加最后一位 '\0'

    char string_01[] = "0123456";
    printf("%s\n", string_01);                                  // 0123456
    printf("%lu\n", sizeof(string_01) / sizeof(string_01[0]));  // 8
    printf("\n");

    // 字符串常量存储在文字常量区中
    char *p_01 = "0123456";
    printf("p_01 存储的字符串为: %s\n", p_01); // 0123456
    printf("p_01 指向的地址为: %p\n", p_01);
    printf("\n");

    printf("p_01 指针的大小为: %d\n", sizeof(p_01));  // 8
    printf("字符串常量 \"0123456789\" 的大小为: %d\n", sizeof("0123456"));   // 8
    printf("\n");

    // 字符串常量区存储的内容不能进行修改
    // *p_01 = '9'; // 程序出错

    char char_array_01[1024] = "Hello";
    char char_array_02[1024] = "World";

    char *p_02 = connectCharArray(char_array_01, char_array_02);
    printf("%s\n", p_02);   // HelloWorld
    printf("\n");
    free(p_02);

    return 0;
}

char *connectCharArray(const char *string_01, const char *string_02) {
    char *connect_array = malloc(sizeof(char) * 1024);

    int i = 0;
    while (string_01[i] != '\0') {
        connect_array[i] = string_01[i];
        i++;
    }

    int j = 0;
    while (string_02[j] != '\0') {
        connect_array[i + j] = string_02[j];
        j++;
    }

    connect_array[i + j] = '\0';

    return connect_array;
}